/** @file gui_group.cpp
    @brief A groupbox widget */

#include "gui/gui_group.h"

//==============================================================================
// Constructors
//==============================================================================
G3::GUI::GROUP::GROUP () {
    Color = COL4F (1, 1, 1, 1);
    FrameVisible = true;
}

//==============================================================================
// Destructors
//==============================================================================

// Just chill?

//==============================================================================
// = operators
//==============================================================================
G3::GUI::GROUP& G3::GUI::GROUP::operator= (const GROUP &aGroup) {
    Visible = aGroup.Visible;
    Rect = aGroup.Rect;
    Children = aGroup.Children;

    Color = aGroup.Color;
    FrameVisible = aGroup.FrameVisible;

    return *this;
}

//==============================================================================
// Creates the group
//==============================================================================
void G3::GUI::GROUP::Create (const RECTI &aRect, FONT *aFont, const std::string &aTitle) {
    if (aFont != NULL)
        SetCaption (aTitle);

    if (aFont != NULL)
        AttachFont (aFont);

    SetRect (aRect);
}

//==============================================================================
// Sets group dimensions
//==============================================================================
void G3::GUI::GROUP::SetRect (const RECTI &aRect) {
    Rect.Left = aRect.Left;
    Rect.Top = aRect.Top;

    if (!Children.empty () && Children [0]) {
        Children [0]->SetPosition (VEC2I (Rect.Left + 7, Rect.Top));

        Rect.Right = G3_MAX (aRect.Right, Children [0]->Rect.Right + 7);
        Rect.Bottom = G3_MAX (aRect.Bottom, Children [0]->Rect.Bottom + 2);
    } else {
        Rect.Right = aRect.Right;
        Rect.Bottom = aRect.Bottom;
    }
}

//==============================================================================
// Attaches a font to the group label
//==============================================================================
void G3::GUI::GROUP::AttachFont (FONT *aFont) {
    if (!Children.empty () && Children [0])
        ((LABEL *) Children [0])->AttachFont (aFont);
}

//==============================================================================
// Sets group label value
//==============================================================================
void G3::GUI::GROUP::SetCaption (const string &aCaption) {
    if (Children.empty () || !Children [0]) {
        Children.push_back (new LABEL);

        Assert (Children [0], "GROUP::SetCaption: std::vector push_back failed");
        Children [0]->SetPosition (VEC2I (Rect.Left + 7, Rect.Top));
    }

    ((LABEL *) Children [0])->SetValue (aCaption);

    Rect.Right = G3_MAX (Rect.Right, Children [0]->Rect.Right + 7);
    Rect.Bottom = G3_MAX (Rect.Bottom, Children [0]->Rect.Bottom + 2);
}

//==============================================================================
// Draws the group
//==============================================================================
void G3::GUI::GROUP::Draw (float aElapsedTime) {
    if (!Visible)
        return;

    glPushMatrix ();
    glDisable (GL_TEXTURE_2D);

    if (FrameVisible) {
        // Group frame color
        glColor4f (Color.r, Color.g, Color.b, Color.a);

        if (!Children.empty () && Children [0]) {
            glBegin (GL_LINE_STRIP);
            glVertex2i (Rect.Left + 5, -Rect.Top - (Children [0]->Rect.Bottom - Children [0]->Rect.Top) / 2);
            glVertex2i (Rect.Left, -Rect.Top - (Children [0]->Rect.Bottom - Children [0]->Rect.Top) / 2);
            glVertex2i (Rect.Left, -Rect.Bottom);
            glVertex2i (Rect.Right, -Rect.Bottom);
            glVertex2i (Rect.Right, -Rect.Top - (Children [0]->Rect.Bottom - Children [0]->Rect.Top) / 2);
            glVertex2i (Children [0]->Rect.Right + 5, -Rect.Top - (Children [0]->Rect.Bottom - Children [0]->Rect.Top) / 2);
            glEnd ();

            glEnable (GL_TEXTURE_2D);

            Assert (((LABEL *) Children [0])->Font, "GROUP::Draw: Label Font member is a NULL pointer");

            Children [0]->Draw (aElapsedTime);
        } else {
            glBegin (GL_LINE_STRIP);
            glVertex2i (Rect.Left, -Rect.Top);
            glVertex2i (Rect.Left, -Rect.Bottom);
            glVertex2i (Rect.Right, -Rect.Bottom);
            glVertex2i (Rect.Right, -Rect.Top);
            glVertex2i (Rect.Left, -Rect.Top);
            glEnd ();

            glEnable (GL_TEXTURE_2D);
        }
    }

    glPopMatrix ();
}
